﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Asteroids
{
    class Ship : GameObject
    {
        public int Health { get; set; } // Current health
        public bool IsAlive { get; set; } // Is the player alive?

        public Ship(Game g, Vector2 position)
            : base(g)
        {
            Position = position;
            Velocity = new Vector2(0, 0);
            Rotation = (float)-1.58;
            Health = 100;           //starts with full health bar
            IsAlive = true;
            collided();
        }
    
        Texture2D sprite;
        SpriteBatch spriteBatch;
        SoundEffect soundEffectDamaged;
        SoundEffect soundEffectDead;

        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("Asteroid-Ship-2");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            soundEffectDamaged = Game.Content.Load<SoundEffect>("damage");
            soundEffectDead = Game.Content.Load<SoundEffect>("ship_explosion");
        }

        public override void Draw(GameTime gameTime)
        {
            float scale = 1.0f;

            Vector2 offset =
             new Vector2((float)sprite.Width / 2f,
                         (float)sprite.Height / 2f);

            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
            
            Position += Velocity * elapsedSeconds;              // Updates ship's position

            wrapAround(800, 500, sprite.Width/2f * scale, sprite.Height/2f * scale);     //Wraps around if obj goes off-screen

            boundingRectangle = new Rectangle((int)Position.X - (int)offset.X,      //Updates obj's boundingRectangle
                                              (int)Position.Y - (int)offset.Y,
                                              (int) (sprite.Width * scale),
                                              (int) (sprite.Height * scale));

            spriteBatch.Begin();
            spriteBatch.Draw(sprite,
                             Position,
                             null,
                             Color.White,
                             Rotation,
                             offset,
                             scale,
                             SpriteEffects.None,
                             0);
            spriteBatch.End();
        }

        public void moveFwd(float elapsedSeconds)
        // Accelerates ship
        {
            Vector2 unitVector =
                new Vector2(
                    (float)Math.Cos(Rotation),
                    (float)Math.Sin(Rotation)
                );

            Velocity += 250f * elapsedSeconds * unitVector;     //Updates ship's velocity
        }

        public void moveTwdsSun(Vector2 SunPos, float elapsedSeconds)
        {
            Vector2 unitVector = (SunPos - Position + new Vector2(30, 30));
            unitVector.Normalize();
            Velocity += 100f * elapsedSeconds * unitVector;
            Position += Velocity * elapsedSeconds;
        }

        //If player is hurt, subtract health
        public void HealthInjury(int x)
        {
            soundEffectDamaged.Play();
            Health -= x;
            if (Health <= 0)
            {
                IsAlive = false;
                soundEffectDead.Play();
                Health = 0;
            }
                
        }

    }
}
